package com.privatbank.Dao;

import com.privatbank.tables.Manufacturer;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Slava on 26.07.2014.
 */
public class ManufacturerDao {
    private final String login;
    private final String password;
    private final String url;

    public ManufacturerDao(String login, String password, String url) {
        this.login = login;
        this.password = password;
        this.url = url;
    }

    public List<Manufacturer> getAll() throws SQLException {
        List<Manufacturer> manufacturers = new ArrayList<Manufacturer>();
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, login, password);
            stmt = con.prepareStatement("SELECT * FROM manufacturer");
            rs = stmt.executeQuery();
            while (rs.next()) {
                Manufacturer man = new Manufacturer();
                man.setId(rs.getInt(1));
                man.setName(rs.getString(2));
                manufacturers.add(man);
            }
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        return manufacturers;
    }
}
